<script lang="ts" setup>
import { PeaceColumn } from '@/components/type/PeaceColumn'
import { ComponentEnum } from '@/enum/ComponentEnum'
import { NFlex } from 'naive-ui'
import { ifFilterUndefinedRepeatNum } from '@/infrastructure/PeaceDataTable/util/PeaceColumnUtil.ts'
import { ObjectUtil } from '@/util/ObjectUtil'

const props = withDefaults(
  defineProps<{
    peaceItem: PeaceColumn
    index: number
    filterModel: Record<string, unknown>
  }>(),
  {
    show: false,
  },
)
// const emit = defineEmits<{
//   (e: 'update:filter-model', value: Record<string, unknown>): void
// }>()

// const modelValue = ref<Record<string, unknown>>(JSON.parse(JSON.stringify(props.filterModel)))
// watch(modelValue, (newModelVal) => {
//   emit("update:filter-model",newModelVal)
// },{deep:true,immediate:true})
const modelValue = computed({
  get:()=> props.filterModel,
})
const ifSingleComponent = computed(()=>{
  return props.peaceItem?.filterConfig?.componentEnum
    && props.peaceItem.filterConfig?.componentEnum === ComponentEnum.CASCADER
    && ObjectUtil.isNullOrUndefined(props.peaceItem?.filterConfig?.peaceRender)
    && !ifFilterUndefinedRepeatNum(props.peaceItem)
})
</script>
<template>
  <template v-if="ifSingleComponent">
    <n-flex vertical justify="left">
      <label>{{ peaceItem?.title as string }}</label>
      <template v-for="(numVal,numIndex) in peaceItem.filterConfig?.repeatRender?.num" :key="index + '_' + numIndex">
        <n-flex>
          <template>
            <n-cascader remote v-model:value="modelValue[peaceItem.filterConfig?.repeatRender?.returnFileNames[numIndex]] as string | number | Array<number | string> | null"
                        :options="peaceItem.filterConfig.optionConfig?.peaceOption as CascaderOption[] | undefined"
                        :label-field="peaceItem.filterConfig.optionConfig?.peaceLabelField ?? 'label'"
                        :value-field="peaceItem.filterConfig.optionConfig?.peaceValueField ?? 'value'"
                        :children-field="peaceItem.filterConfig.optionConfig?.peaceChildField ?? 'children'"
                        :placeholder="('请选择' + peaceItem?.title) as string"
                        check-strategy="child"
                        clearable
                        expand-trigger="hover"
                        class="w-200px"
            >
            </n-cascader>
          </template>
        </n-flex>
      </template>
    </n-flex>
  </template>
</template>
<style scoped></style>
